package dhm.com.hpx.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import dhm.com.hpx.R;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.entity.DynamicListaBean;
import dhm.com.hpx.utils.SpUtils;

/**
 * Created by admin
 * 2019/4/29
 * 所有订单适配器
 */
public class DynamicAdapter extends RecyclerView.Adapter<DynamicAdapter.ViewHolder> {

    private Context context;


    List<DynamicListaBean.DataBean> list=new ArrayList<>();

    public DynamicAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<DynamicListaBean.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewHolder viewHolder;
        View view= LayoutInflater.from(context).inflate(R.layout.item_dynamic, viewGroup,false);
        viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.image.setImageURI(Constant.PATH+list.get(i).getImage());
        viewHolder.nick.setText(list.get(i).getNick());
        viewHolder.content.setText(list.get(i).getDynamic_con());
        viewHolder.time.setText(list.get(i).getCreate_time());

        GridLayoutManager manager=new GridLayoutManager(context,3){
            @Override
            public boolean canScrollVertically() {
                return false;
            }

        };
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        viewHolder.recy.setLayoutManager(manager);
        DynamicImageAdapter adapter=new DynamicImageAdapter(context);
        viewHolder.recy.setAdapter(adapter);
        for (int j = 0; j < list.get(i).getDynamic_pic().size(); j++) {
            if (list.get(i).getDynamic_pic().get(j).equals("")){
                list.get(i).getDynamic_pic().remove(j);
                j--;
            }
        }

        if (list.get(i).getDynamic_pic()!=null&&list.get(i).getDynamic_pic().size()>0){
            viewHolder.recy.setVisibility(View.VISIBLE);
            adapter.showImage(list.get(i).getDynamic_pic());
        }else {
            viewHolder.recy.setVisibility(View.GONE);
        }

        String uid = SpUtils.getString(context, "uid");
        if (uid.equals(list.get(i).getUser_id()+"")){
            viewHolder.del.setVisibility(View.VISIBLE);
            viewHolder.del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onClick.item(i,view);
                }
            });
        }else {
            viewHolder.del.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public SimpleDraweeView image;
        public TextView nick;
        public TextView content;
        public RecyclerView recy;
        public TextView time;
        public ImageView del;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image=itemView.findViewById(R.id.image);
            nick=itemView.findViewById(R.id.nick);
            content=itemView.findViewById(R.id.content);
            recy=itemView.findViewById(R.id.recy);
            time=itemView.findViewById(R.id.time);
            del=itemView.findViewById(R.id.del);
        }
    }

    private OnClick onClick;
    public void setdeleteClick(OnClick onClick) {
        this.onClick = onClick;
    }
    public interface OnClick{
        void item(int posttion,View view);
    }

    private OnIntentClick onIntentClick;
    public void setaddClick(OnIntentClick onClick) {
        this.onIntentClick = onClick;
    }
    public interface OnIntentClick{
        void item();
    }

}

